<div class="layuimini-container layuimini-page-anim">
    <div class="layuimini-main">
        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" action="" lay-filter="search">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">月份</label>
                            <div class="layui-input-inline">
                                <input type="text" name="month" autocomplete="off" class="layui-input" id="month">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">类型</label>
                            <div class="layui-input-inline">
                                <select name="type" lay-verify="" id="type">
                                    <option value="0">个人</option>
                                    <option value="1">家庭</option>
                                </select>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <div class="layui-row layui-col-space15" style="margin-top: 10px">
            <div class="layui-col-md12">
                <div class="layui-card">
                    <div class="layui-card-header"><i class="fa fa-line-chart icon"></i>当月统计</div>
                    <div class="layui-card-body">
                        <div layui-row layui-col-space15>
                            <div class="layui-col-md6">
                                <div id="pay" style="width: 100%;min-height:250px"></div>
                            </div>
                            <div class="layui-col-md6">
                                <div id="income" style="width: 100%;min-height:250px"></div>
                            </div>
                        </div>
                        <div layui-row layui-col-space15>
                            <div class="layui-col-md6">
                                <div id="invest" style="width: 100%;min-height:250px"></div>
                            </div>
                            <div class="layui-col-md6">
                                <div id="loan" style="width: 100%;min-height:250px"></div>
                            </div>
                        </div>

                    </div>
                </div>
            </div>
        </div>

    </div>
</div>

<script>
    layui.use(['form', 'laydate','miniPage', 'axios','echarts','layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            laydate = layui.laydate,
            echarts = layui.echarts,
            layer = layui.layer,
            axios = layui.axios,
            miniPage = layui.miniPage;

        laydate.render({
            elem: '#month',
            type: 'month',
            value: new Date()
        })

        form.render(null, 'search');

        //渲染本月的情况
        //支出
        var chartDom = document.getElementById('pay');
        var pay = echarts.init(chartDom);
        var option;
        option = {
            title: {
                text: '本月支出统计',
                // subtext: '纯属虚构',
                left: 'center'
            },
            tooltip: {
                trigger: 'item'
            },
            legend: {
                orient: 'vertical',
                left: 'left',
            },
            series: [
                {
                    name: '支出统计',
                    type: 'pie',
                    radius: '50%',
                    data: [
                        // {value: 1048, name: '支出'},
                        // {value: 735, name: '收入'},
                        // {value: 580, name: '投资'},
                        // {value: 484, name: '借贷'}
                    ],
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        };
        option && pay.setOption(option);

        //收入
        var chartDom = document.getElementById('income');
        var income = echarts.init(chartDom);
        var option;
        option = {
            title: {
                text: '本月收入统计',
                // subtext: '纯属虚构',
                left: 'center'
            },
            tooltip: {
                trigger: 'item'
            },
            legend: {
                orient: 'vertical',
                left: 'left',
            },
            series: [
                {
                    name: '收入统计',
                    type: 'pie',
                    radius: '50%',
                    data: [
                        // {value: 1048, name: '支出'},
                        // {value: 735, name: '收入'},
                        // {value: 580, name: '投资'},
                        // {value: 484, name: '借贷'}
                    ],
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        };
        option && income.setOption(option);

        //投资
        var chartDom = document.getElementById('invest');
        var invest = echarts.init(chartDom);
        var option;
        option = {
            title: {
                text: '本月投资统计',
                // subtext: '纯属虚构',
                left: 'center'
            },
            tooltip: {
                trigger: 'item'
            },
            legend: {
                orient: 'vertical',
                left: 'left',
            },
            series: [
                {
                    name: '投资统计',
                    type: 'pie',
                    radius: '50%',
                    data: [
                        // {value: 1048, name: '支出'},
                        // {value: 735, name: '收入'},
                        // {value: 580, name: '投资'},
                        // {value: 484, name: '借贷'}
                    ],
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        };
        option && invest.setOption(option);

        //借贷
        var chartDom = document.getElementById('loan');
        var loan = echarts.init(chartDom);
        var option;
        option = {
            title: {
                text: '本月借贷统计',
                // subtext: '纯属虚构',
                left: 'center'
            },
            tooltip: {
                trigger: 'item'
            },
            legend: {
                orient: 'vertical',
                left: 'left',
            },
            series: [
                {
                    name: '借贷统计',
                    type: 'pie',
                    radius: '50%',
                    data: [
                        // {value: 1048, name: '支出'},
                        // {value: 735, name: '收入'},
                        // {value: 580, name: '投资'},
                        // {value: 484, name: '借贷'}
                    ],
                    emphasis: {
                        itemStyle: {
                            shadowBlur: 10,
                            shadowOffsetX: 0,
                            shadowColor: 'rgba(0, 0, 0, 0.5)'
                        }
                    }
                }
            ]
        };
        option && loan.setOption(option);

        //data包含两个属性：year(查询的年份), type(查询的类型，0代表个人， 1代表家庭)
        function init(data){
            //请求个人当前年份的统计
            axios({
                url: 'statistic/monthData',
                method: 'post',
                data: data
            }).then(response => {
                console.log(response.data)
                let payOption = {
                    series: [
                        {
                            data: response.data.payData
                        }
                    ]
                }
                let incomeOption = {
                    series: [
                        {
                            data: response.data.incomeData
                        }
                    ]
                }
                let investOption = {
                    series: [
                        {
                            data: response.data.investData
                        }
                    ]
                }
                let loanOption = {
                    series: [
                        {
                            data: response.data.loanData
                        }
                    ]
                }
                pay.setOption(payOption)
                income.setOption(incomeOption)
                invest.setOption(investOption)
                loan.setOption(loanOption)
            }).catch(error => {
                console.log(error)
            })

        }

        let today = new Date();
        let initData = {
            year: today.getFullYear(),
            month: today.getMonth(),
            type: 0
        }

        init(initData);

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            let month = data.field.month;
            let res = month.split("-");

            let param = {
                year: res[0],
                month: res[1] - 1,
                type: data.field.type
            }
            init(param);
            return false;
        });
    });
</script>